﻿using CPlatePrintSys.Server.Models;
using FluentMigrator;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CPlatePrintSys.Server.Migrations
{
    /// <summary>
    /// 使用FluentMigrator 初始化数据库
    /// </summary>
    [Migration(20251106153830001, "创建ASPNET用户角色表")]
    public class Mig20251106153830001_InitialCreate: Migration
    {
        public override void Up()
        {
            // Create AspNetRoles table
            if (!Schema.Table("AspNetRoles").Exists())
            {
                Create.Table("AspNetRoles")
                    .WithColumn("Id").AsString(128).NotNullable().PrimaryKey()
                    .WithColumn("Name").AsString(256).NotNullable();
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetRoles").Exists() && !Schema.Table("AspNetRoles").Index("RoleNameIndex").Exists())
            {
                Create.Index("RoleNameIndex")
                    .OnTable("AspNetRoles")
                    .OnColumn("Name")
                    .Unique();
            }

            // Create AspNetUsers table
            if (!Schema.Table("AspNetUsers").Exists())
            {
                Create.Table("AspNetUsers")
                    .WithColumn("Id").AsString(128).NotNullable().PrimaryKey()
                    .WithColumn("FullName").AsString().Nullable()
                    .WithColumn("Email").AsString(256).Nullable()
                    .WithColumn("EmailConfirmed").AsBoolean().NotNullable()
                    .WithColumn("PasswordHash").AsString().Nullable()
                    .WithColumn("SecurityStamp").AsString().Nullable()
                    .WithColumn("PhoneNumber").AsString().Nullable()
                    .WithColumn("PhoneNumberConfirmed").AsBoolean().NotNullable()
                    .WithColumn("TwoFactorEnabled").AsBoolean().NotNullable()
                    .WithColumn("LockoutEndDateUtc").AsDateTime().Nullable()
                    .WithColumn("LockoutEnabled").AsBoolean().NotNullable()
                    .WithColumn("AccessFailedCount").AsInt32().NotNullable()
                    .WithColumn("UserName").AsString(256).NotNullable();
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetUsers").Exists() && !Schema.Table("AspNetUsers").Index("UserNameIndex").Exists())
            {
                Create.Index("UserNameIndex")
                    .OnTable("AspNetUsers")
                    .OnColumn("UserName")
                    .Unique();
            }

            // Create AspNetUserClaims table
            if (!Schema.Table("AspNetUserClaims").Exists())
            {
                Create.Table("AspNetUserClaims")
                    .WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
                    .WithColumn("UserId").AsString(128).NotNullable()
                    .WithColumn("ClaimType").AsString().Nullable()
                    .WithColumn("ClaimValue").AsString().Nullable();
            }

            // 检查外键 (此语法在上一版中是正确的)
            if (Schema.Table("AspNetUserClaims").Exists() && !Schema.Table("AspNetUserClaims").Constraint("FK_AspNetUserClaims_AspNetUsers_UserId").Exists())
            {
                Create.ForeignKey("FK_AspNetUserClaims_AspNetUsers_UserId")
                    .FromTable("AspNetUserClaims").ForeignColumn("UserId")
                    .ToTable("AspNetUsers").PrimaryColumn("Id")
                    .OnDelete(Rule.Cascade);
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetUserClaims").Exists() && !Schema.Table("AspNetUserClaims").Index("IX_AspNetUserClaims_UserId").Exists())
            {
                Create.Index("IX_AspNetUserClaims_UserId")
                    .OnTable("AspNetUserClaims")
                    .OnColumn("UserId");
            }

            // Create AspNetUserLogins table
            if (!Schema.Table("AspNetUserLogins").Exists())
            {
                Create.Table("AspNetUserLogins")
                    .WithColumn("LoginProvider").AsString(128).NotNullable()
                    .WithColumn("ProviderKey").AsString(128).NotNullable()
                    .WithColumn("UserId").AsString(128).NotNullable();
            }

            // 检查主键 (此语法在上一版中是正确的)
            if (Schema.Table("AspNetUserLogins").Exists() && !Schema.Table("AspNetUserLogins").Constraint("PK_AspNetUserLogins").Exists())
            {
                Create.PrimaryKey("PK_AspNetUserLogins")
                    .OnTable("AspNetUserLogins")
                    .Columns("LoginProvider", "ProviderKey", "UserId");
            }

            // 检查外键
            if (Schema.Table("AspNetUserLogins").Exists() && !Schema.Table("AspNetUserLogins").Constraint("FK_AspNetUserLogins_AspNetUsers_UserId").Exists())
            {
                Create.ForeignKey("FK_AspNetUserLogins_AspNetUsers_UserId")
                    .FromTable("AspNetUserLogins").ForeignColumn("UserId")
                    .ToTable("AspNetUsers").PrimaryColumn("Id")
                    .OnDelete(Rule.Cascade);
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetUserLogins").Exists() && !Schema.Table("AspNetUserLogins").Index("IX_AspNetUserLogins_UserId").Exists())
            {
                Create.Index("IX_AspNetUserLogins_UserId")
                    .OnTable("AspNetUserLogins")
                    .OnColumn("UserId");
            }

            // Create AspNetUserRoles table
            if (!Schema.Table("AspNetUserRoles").Exists())
            {
                Create.Table("AspNetUserRoles")
                    .WithColumn("UserId").AsString(128).NotNullable()
                    .WithColumn("RoleId").AsString(128).NotNullable();
            }

            // 检查主键
            if (Schema.Table("AspNetUserRoles").Exists() && !Schema.Table("AspNetUserRoles").Constraint("PK_AspNetUserRoles").Exists())
            {
                Create.PrimaryKey("PK_AspNetUserRoles")
                    .OnTable("AspNetUserRoles")
                    .Columns("UserId", "RoleId");
            }

            // 检查外键
            if (Schema.Table("AspNetUserRoles").Exists() && !Schema.Table("AspNetUserRoles").Constraint("FK_AspNetUserRoles_AspNetRoles_RoleId").Exists())
            {
                Create.ForeignKey("FK_AspNetUserRoles_AspNetRoles_RoleId")
                    .FromTable("AspNetUserRoles").ForeignColumn("RoleId")
                    .ToTable("AspNetRoles").PrimaryColumn("Id")
                    .OnDelete(Rule.Cascade);
            }

            // 检查外键
            if (Schema.Table("AspNetUserRoles").Exists() && !Schema.Table("AspNetUserRoles").Constraint("FK_AspNetUserRoles_AspNetUsers_UserId").Exists())
            {
                Create.ForeignKey("FK_AspNetUserRoles_AspNetUsers_UserId")
                    .FromTable("AspNetUserRoles").ForeignColumn("UserId")
                    .ToTable("AspNetUsers").PrimaryColumn("Id")
                    .OnDelete(Rule.Cascade);
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetUserRoles").Exists() && !Schema.Table("AspNetUserRoles").Index("IX_AspNetUserRoles_RoleId").Exists())
            {
                Create.Index("IX_AspNetUserRoles_RoleId")
                    .OnTable("AspNetUserRoles")
                    .OnColumn("RoleId");
            }

            // 修正: 使用 Schema.Table(...).Index(...).Exists()
            if (Schema.Table("AspNetUserRoles").Exists() && !Schema.Table("AspNetUserRoles").Index("IX_AspNetUserRoles_UserId").Exists())
            {
                Create.Index("IX_AspNetUserRoles_UserId")
                    .OnTable("AspNetUserRoles")
                    .OnColumn("UserId");
            }
        }

        public override void Down()
        {
            // Drop AspNetUserRoles table and related objects
            if (Schema.Table("AspNetUserRoles").Constraint("FK_AspNetUserRoles_AspNetRoles_RoleId").Exists())
                Delete.ForeignKey("FK_AspNetUserRoles_AspNetRoles_RoleId").OnTable("AspNetUserRoles"); // 明确指定表

            if (Schema.Table("AspNetUserRoles").Constraint("FK_AspNetUserRoles_AspNetUsers_UserId").Exists())
                Delete.ForeignKey("FK_AspNetUserRoles_AspNetUsers_UserId").OnTable("AspNetUserRoles"); // 明确指定表

            if (Schema.Table("AspNetUserRoles").Index("IX_AspNetUserRoles_RoleId").Exists())
                Delete.Index("IX_AspNetUserRoles_RoleId").OnTable("AspNetUserRoles");

            if (Schema.Table("AspNetUserRoles").Index("IX_AspNetUserRoles_UserId").Exists())
                Delete.Index("IX_AspNetUserRoles_UserId").OnTable("AspNetUserRoles");

            if (Schema.Table("AspNetUserRoles").Constraint("PK_AspNetUserRoles").Exists())
                Delete.PrimaryKey("PK_AspNetUserRoles").FromTable("AspNetUserRoles");

            if (Schema.Table("AspNetUserRoles").Exists())
                Delete.Table("AspNetUserRoles");

            // Drop AspNetUserLogins table and related objects
            if (Schema.Table("AspNetUserLogins").Constraint("FK_AspNetUserLogins_AspNetUsers_UserId").Exists())
                Delete.ForeignKey("FK_AspNetUserLogins_AspNetUsers_UserId").OnTable("AspNetUserLogins");

            if (Schema.Table("AspNetUserLogins").Index("IX_AspNetUserLogins_UserId").Exists())
                Delete.Index("IX_AspNetUserLogins_UserId").OnTable("AspNetUserLogins");

            if (Schema.Table("AspNetUserLogins").Constraint("PK_AspNetUserLogins").Exists())
                Delete.PrimaryKey("PK_AspNetUserLogins").FromTable("AspNetUserLogins");

            if (Schema.Table("AspNetUserLogins").Exists())
                Delete.Table("AspNetUserLogins");

            // Drop AspNetUserClaims table and related objects
            if (Schema.Table("AspNetUserClaims").Constraint("FK_AspNetUserClaims_AspNetUsers_UserId").Exists())
                Delete.ForeignKey("FK_AspNetUserClaims_AspNetUsers_UserId").OnTable("AspNetUserClaims");

            if (Schema.Table("AspNetUserClaims").Index("IX_AspNetUserClaims_UserId").Exists())
                Delete.Index("IX_AspNetUserClaims_UserId").OnTable("AspNetUserClaims");

            if (Schema.Table("AspNetUserClaims").Exists())
                Delete.Table("AspNetUserClaims");

            // Drop AspNetUsers table and related objects
            if (Schema.Table("AspNetUsers").Index("UserNameIndex").Exists())
                Delete.Index("UserNameIndex").OnTable("AspNetUsers");

            if (Schema.Table("AspNetUsers").Exists())
                Delete.Table("AspNetUsers");

            // Drop AspNetRoles table and related objects
            if (Schema.Table("AspNetRoles").Index("RoleNameIndex").Exists())
                Delete.Index("RoleNameIndex").OnTable("AspNetRoles");

            if (Schema.Table("AspNetRoles").Exists())
                Delete.Table("AspNetRoles");
        }

    }
}
